const path = require('path')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  devServer: {
    // proxy: {
    //   '/kz': {
    //     target: 'http://api.52pp365.com/',
    //     changeOrigin: true,
    //     pathRewrite: {
    //       '^/kz': '/kz'
    //     }
    //   }
    // }
  },
  publicPath: './', // 打包后引用的资源路径
  // outputDir: 'dist', // 打包目录
  configureWebpack: config => {
    // webpack自定义配置
    // 生产环境打包分析体积
    if (process.env.NODE_ENV === 'production' && process.env.npm_config_report) {
      return {
        plugins: [new BundleAnalyzerPlugin()]
      }
    }
  },
  chainWebpack: config => {
    // CLI内部webpack配置
    const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
    types.forEach(type => addStyleResource(config.module.rule('less').oneOf(type)))
  }
}

// 全局样式 变量、函数
function addStyleResource(rule) {
  rule
    .use('style-resource')
    .loader('style-resources-loader')
    .options({
      patterns: [resolve('src/styles/variables.less'), resolve('src/styles/mixin.less')]
    })
}
